package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.core.file.DetailFileVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.FileDao;
import com.zhuss.exception.OceanException;

/**
 * 文件下载 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/25 19:57
 */
@Component("downloadProcessor")
public class DownloadProcessor extends AbstractProcessor<DownloadVo, DownloadBo> implements Processor<DownloadVo, DownloadBo> {

    
	@Value("${fileReadPath}")
    private String fileReadPath;
	
	@Autowired
	private FileDao fileDao;
	
    /**
     * 校验
     */
    @Override
    public void check(DownloadBo bo) throws OceanException {
    }
    
    /**
     * 操作方法
     * @throws OceanException 
     */
    @Override
    public DownloadVo process(DownloadBo bo) throws OceanException {
    	String key = bo.getKey();
    	List<String> keyList = bo.getKeyList();
    	// 根据key查询出来 url
    	DownloadVo vo = new DownloadVo();
    	if (key != null && !"".equals(key) && !key.contains(",")) {
    		vo.setUrl(fileReadPath + key);
    		DetailFileVo file = fileDao.getByKey(key);
    		if (file != null) {
    			vo.setFileName(file.getFileName());
    		}
    	}
    	if (key != null && !"".equals(key)) {
    		List<String> urlList = new ArrayList<>();
    		List<String> fileNameList = new ArrayList<String>();
    		for (String item : key.split(",")) {
    			urlList.add(fileReadPath + item);
    			DetailFileVo file = fileDao.getByKey(item);
    			if (file != null) {
    				fileNameList.add(file.getFileName());
    			}
    		}
    		vo.setUrlList(urlList);
    		vo.setFileNameList(fileNameList);
    	}
    	if (keyList != null && !keyList.isEmpty()) {
    		List<String> urlList = new ArrayList<>();
    		List<String> fileNameList = new ArrayList<>();
    		for (String item : keyList) {
    			urlList.add(fileReadPath + item);
    			DetailFileVo file = fileDao.getByKey(item);
    			if (file != null) {
    				fileNameList.add(file.getFileName());
    			}
    		}
    		vo.setUrlList(urlList);
    		vo.setFileNameList(fileNameList);
    	}
    	
        return vo;
    }
    
    /**
     * 操作日志
     */
	@Override
	public DownloadVo log(DownloadBo param) throws OceanException {
		/*OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.文件下载.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(null);
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);*/
    	return null;
	}
}
